package com.itheima.movement.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.common.ThreadLocalUtil;
import com.itheima.entity.pojo.MovementLike;
import com.itheima.entity.pojo.MovementLike;
import com.itheima.movement.mapper.MovementLikeMapper;
import com.itheima.movement.service.MovementLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author :wujunjie
 * createTime :2022-12-23 14:32
 * description :
 */
@Service
public class MovementLikeServiceImpl implements MovementLikeService {
    @Autowired
    private MovementLikeMapper movementLikeMapper;
    @Override
    public List<MovementLike> selectByMovementIds(ArrayList<String> movementIdList) {
        return movementLikeMapper.selectByMovementIds(movementIdList);
    }

    @Override
    public Boolean insertLike(String movementId) {
        MovementLike movementLikeInDB=movementLikeMapper.selectByMovementIdAndUserId(movementId,ThreadLocalUtil.getId());
        if (!ObjectUtil.isEmpty(movementLikeInDB)){
            throw new RuntimeException("不能重复点赞");
        }
        MovementLike movementLike = new MovementLike();
        movementLike.setMovementId(movementId);
        movementLike.setId(IdUtil.getSnowflakeNextIdStr());
        movementLike.setCreateTime(LocalDateTime.now());
        movementLike.setUserId(ThreadLocalUtil.getId());
        return movementLikeMapper.insertLike(movementLike);
    }

    @Override
    public Boolean deleteLike(String id) {
        return movementLikeMapper.deleteLike(id);
    }
}
